package com.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.domain.SysFile;
import com.ssm.domain.SysLog;
import com.ssm.service.ISysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 日志控制类
 */
@Controller
@RequestMapping("/sysLog")
public class SysLogController {

    @Autowired
    private ISysLogService sysLogService;

    //分页
    @RequestMapping("/findAll.do")
    public ModelAndView findAll(@RequestParam(name="page",required = true, defaultValue = "1") Integer page,
                                @RequestParam(name="size",required = true, defaultValue = "5") Integer size)
            throws Exception {
        ModelAndView mv=new ModelAndView();
        List<SysLog> syslog = sysLogService.findAll(page, size);
        //pageInfo就是一个分页Bean
        PageInfo pageInfo = new PageInfo(syslog);
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("syslog-page-list");
        return mv;
    }

    @RequestMapping("/findAllsortByDate.do")
    public ModelAndView findAllsortByDate(@RequestParam(name="page",required = true, defaultValue = "1") Integer page,
                                          @RequestParam(name="size",required = true, defaultValue = "5") Integer size)
            throws Exception {
        ModelAndView mv = new ModelAndView();
        List<SysLog> fileList = sysLogService.findAllsortByDate(page,size);
        PageInfo pageInfo = new PageInfo(fileList);
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("syslog-page-list2");
        return mv;
    }

    @RequestMapping("/deleteAll.do")
    @PreAuthorize("authentication.principal.username == 'admin'")
    public String deleteAll() throws Exception {
        sysLogService.deleteAll();
        return "redirect:findAll.do";
    }

    @RequestMapping("/deleteSelect.do")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public String deleteSelect(@RequestParam(name="ids", required = true)String[] syslogIds) throws Exception {
        sysLogService.deleteSelect(syslogIds);
        return "redirect:findAll.do";
    }
}
